/*
 * $Id: PolisGroupTestCase.java 27 2007-04-13 01:02:48Z aaron.day $
 */
package org.crazydays.core.data;


import java.util.Set;
import java.util.Iterator;

import org.hibernate.Session;
import org.hibernate.HibernateException;

import junit.framework.TestCase;

import org.crazydays.hibernate.HibernateUtil;

/**
 * PolisGroupTestCase
 */
public class PolisGroupTestCase
	extends TestCase
{
	/**
	 * Test guest.
	 */
	public void testGuest()
	{
		Session session = HibernateUtil.getSession();
		try {
			PolisGroup group = (PolisGroup) HibernateUtil
					.loadPolisDataByName(session,
							PolisGroup.class,
							"Guest");

			assertNotNull("group == null", group);

			// TODO: check fields

			// check acls
			assertNotNull("group.acls == null", group.getAcls());
			assertEquals("group.acls.size != 1", group.getAcls()
					.size(), 1);
			Iterator<PolisAcl> acls = group.getAcls().iterator();
			while (acls.hasNext()) {
				PolisAcl acl = acls.next();

				if (acl.getName().equals("Guest")) {
					continue;
				}
				fail("Unknown acl: " + acl.getName());
			}

			// check users
			Set<PolisUser> users = group.getUsers();
			assertNotNull("users == null", users);
			assertEquals("users.size != 1", users.size(), 1);

			Iterator<PolisUser> i = users.iterator();
			while (i.hasNext()) {
				PolisUser user = i.next();

				if (user.getName().equals("Guest")) {
					continue;
				}

				fail("Unknown user: " + user.getName());
			}
		} catch (HibernateException e) {
			fail("HibernateException: " + e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	/**
	 * Test admin.
	 */
	public void testAdmin()
	{
		Session session = HibernateUtil.getSession();
		try {
			PolisGroup group = (PolisGroup) HibernateUtil
					.loadPolisDataByName(session,
							PolisGroup.class,
							"Admin");

			assertNotNull("group == null", group);

			// TODO: check fields

			// check acls
			assertNotNull("group.acls == null", group.getAcls());
			assertEquals("group.acls.size != 3", group.getAcls()
					.size(), 3);
			Iterator<PolisAcl> acls = group.getAcls().iterator();
			while (acls.hasNext()) {
				PolisAcl acl = acls.next();

				if (acl.getName().equals("Guest")) {
					continue;
				}
				if (acl.getName().equals("Admin")) {
					continue;
				}
				if (acl.getName().equals("User")) {
					continue;
				}
				fail("Unknown acl: " + acl.getName());
			}

			// check users
			Set<PolisUser> users = group.getUsers();
			assertNotNull("users == null", users);
			assertEquals("users.size != 1", users.size(), 1);

			Iterator<PolisUser> i = users.iterator();
			while (i.hasNext()) {
				PolisUser user = i.next();

				if (user.getName().equals("Admin")) {
					continue;
				}

				fail("Unknown user: " + user.getName());
			}
		} catch (HibernateException e) {
			fail("HibernateException: " + e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	/**
	 * Test user.
	 */
	public void testUser()
	{
		Session session = HibernateUtil.getSession();
		try {
			PolisGroup group = (PolisGroup) HibernateUtil
					.loadPolisDataByName(session,
							PolisGroup.class,
							"User");

			assertNotNull("group == null", group);

			// TODO: check fields

			// check acls
			assertNotNull("group.acls == null", group.getAcls());
			assertEquals("group.acls.size != 2", group.getAcls()
					.size(), 2);
			Iterator<PolisAcl> acls = group.getAcls().iterator();
			while (acls.hasNext()) {
				PolisAcl acl = acls.next();

				if (acl.getName().equals("Guest")) {
					continue;
				}
				if (acl.getName().equals("User")) {
					continue;
				}
				fail("Unknown acl: " + acl.getName());
			}

			// check users
			Set<PolisUser> users = group.getUsers();
			assertNotNull("users == null", users);
			assertEquals("users.size != 1", users.size(), 1);

			Iterator<PolisUser> i = users.iterator();
			while (i.hasNext()) {
				PolisUser user = i.next();

				if (user.getName().equals("Admin")) {
					continue;
				}

				fail("Unknown user: " + user.getName());
			}
		} catch (HibernateException e) {
			fail("HibernateException: " + e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
}
